package com.xueyi.system.BasePlantKind.controller;

import com.xueyi.system.BasePlantBreed.domain.dto.BasePlantBreedDto;
import com.xueyi.system.BasePlantBreed.domain.query.BasePlantBreedQuery;
import com.xueyi.system.BasePlantBreed.service.IBasePlantBreedService;
import com.xueyi.system.BasePlantKind.domain.query.BasePlantKindQuery;
import com.xueyi.system.BasePlantKind.domain.dto.BasePlantKindDto;
import com.xueyi.system.BasePlantKind.service.IBasePlantKindService;
import com.xueyi.common.web.entity.controller.BaseController;
import com.xueyi.system.plantManager.domain.dto.BasePlantManagerDto;
import com.xueyi.system.plantManager.domain.po.BasePlantManagerPo;
import com.xueyi.system.plantManager.domain.query.BasePlantManagerQuery;
import com.xueyi.system.plantManager.service.IBasePlantManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.core.web.result.AjaxResult;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 种植种类管理 业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/kind")
public class BasePlantKindController extends BaseController<BasePlantKindQuery, BasePlantKindDto, IBasePlantKindService> {


    @Autowired
    private IBasePlantKindService basePlantKindService;

    @Autowired
    private IBasePlantBreedService basePlantBreedService;


    @Autowired
    private IBasePlantManagerService basePlantManagerService;

    /** 定义节点名称 */
    @Override
    protected String getNodeName() {
        return "种植种类" ;
    }

    /**
     * 查询种植种类列表
     */
    @Override
    @GetMapping("/list")
    @RequiresPermissions(Auth.BASE_PLANT_KIND_LIST)
    public AjaxResult list(BasePlantKindQuery basePlantKind) {
        return super.list(basePlantKind);
    }

    /**
     * 查询种植种类详细
     */
    @Override
    @GetMapping(value = "/{id}")
    @RequiresPermissions(Auth.BASE_PLANT_KIND_SINGLE)
    public AjaxResult getInfoExtra(@PathVariable Serializable id) {
        return super.getInfoExtra(id);
    }

    /**
     * 种植种类导出
     */
    @Override
    @PostMapping("/export")
    @RequiresPermissions(Auth.BASE_PLANT_KIND_EXPORT)
    @Log(title = "种植种类管理", businessType = BusinessType.EXPORT)
    public void export(HttpServletResponse response, BasePlantKindQuery basePlantKind) {
        super.export(response, basePlantKind);
    }

    /**
     * 种植种类新增
     */
    @Override
    @PostMapping
    @RequiresPermissions(Auth.BASE_PLANT_KIND_ADD)
    @Log(title = "种植种类管理", businessType = BusinessType.INSERT)
    public AjaxResult add(@Validated({V_A.class}) @RequestBody BasePlantKindDto basePlantKind) {
        return super.add(basePlantKind);
    }

    /**
     * 种植种类修改
     */
    @Override
    @PutMapping
    @RequiresPermissions(Auth.BASE_PLANT_KIND_EDIT)
    @Log(title = "种植种类管理", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@Validated({V_E.class}) @RequestBody BasePlantKindDto basePlantKind) {
        return super.edit(basePlantKind);
    }

    /**
     * 种植种类修改状态
     */
    @Override
    @PutMapping("/status")
    @RequiresPermissions(value = {Auth.BASE_PLANT_KIND_EDIT, Auth.BASE_PLANT_KIND_ES}, logical = Logical.OR)
    @Log(title = "种植种类管理", businessType = BusinessType.UPDATE_STATUS)
    public AjaxResult editStatus(@RequestBody BasePlantKindDto basePlantKind) {
        return super.editStatus(basePlantKind);
    }

    /**
     * 种植种类批量删除
     */
    @Override
    @DeleteMapping("/batch/{idList}")
    @RequiresPermissions(Auth.BASE_PLANT_KIND_DEL)
    @Log(title = "种植种类管理", businessType = BusinessType.DELETE)
    public AjaxResult batchRemove(@PathVariable List<Long> idList) {
        return super.batchRemove(idList);
    }

    /**
     * 种植品种
     * @return
     */
    @GetMapping("/getList")
    public AjaxResult getList(BasePlantKindQuery kindQuery){
        kindQuery.setStatus("0");
        List<BasePlantKindDto> breedDtoList = basePlantKindService.selectListScope(kindQuery);
        BasePlantManagerQuery managerQuery = new BasePlantManagerQuery();
        managerQuery.setPlantStatus("1");
        List<BasePlantManagerDto> dtoList = basePlantManagerService.selectListScope(managerQuery);
        List newlist= (List) dtoList.stream().distinct().collect(Collectors.toList());
        List<BasePlantManagerDto> list = dtoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<BasePlantManagerDto>(Comparator.comparing(BasePlantManagerDto::getPlantVariety))), ArrayList::new));

        for (BasePlantKindDto kindDto : breedDtoList) {
            BasePlantBreedQuery breedQuery = new BasePlantBreedQuery();
            breedQuery.setKindId(kindDto.getId().toString());
            List<BasePlantBreedDto> plantBreedDtoList = basePlantBreedService.selectList(breedQuery);
            kindDto.setBreedList(plantBreedDtoList);
        }

        Map map = new HashMap<>();

        map.put("kind",breedDtoList);
        map.put("plant",list);


        return AjaxResult.success(map);

    }

    /**
     * 获取种植种类选择框列表
     */
    @Override
    @GetMapping("/option")
    public AjaxResult option() {
        return super.option();
    }

    interface Auth {
        /** 系统 - 种植种类管理 - 列表 */
        String BASE_PLANT_KIND_LIST = "system:kind:list" ;
        /** 系统 - 种植种类管理 - 详情 */
        String BASE_PLANT_KIND_SINGLE = "system:kind:single" ;
        /** 系统 - 种植种类管理 - 新增 */
        String BASE_PLANT_KIND_ADD = "system:kind:add" ;
        /** 系统 - 种植种类管理 - 修改 */
        String BASE_PLANT_KIND_EDIT = "system:kind:edit" ;
        /** 系统 - 种植种类管理 - 修改状态 */
        String BASE_PLANT_KIND_ES = "system:kind:es" ;
        /** 系统 - 种植种类管理 - 删除 */
        String BASE_PLANT_KIND_DEL = "system:kind:delete" ;
        /** 系统 - 种植种类管理 - 导出 */
        String BASE_PLANT_KIND_EXPORT = "system:kind:export" ;
    }
}
